﻿using System;
using System.Net;
using System.Windows;
using System.IO.IsolatedStorage;
using TaskHub.EntityDataModel;
using System.Collections.Generic;
using System.Linq;


namespace TaskHub.LocalStorage
{
    public class IsolatedStorageDAO
    {
        #region Properties
        private const string MyIdKey = "myId";
        private const string MyTaskListKey = "myTaskList";
        private const string TaskListKey = "taskList";
        private const string MyContactsKey = "myContactList";
        private static IsolatedStorageSettings IsoSettings = IsolatedStorageSettings.ApplicationSettings;
        #endregion
        
        public static void InitStorage()
        {
            if(!IsoSettings.Contains(MyIdKey)){ IsoSettings[MyIdKey] = 0; }
            if(!IsoSettings.Contains(MyTaskListKey)){ IsoSettings[MyTaskListKey] = new List<TaskEDM>(); }
            if(!IsoSettings.Contains(TaskListKey)){ IsoSettings[TaskListKey] =  new List<TaskEDM>(); }
            if (!IsoSettings.Contains(MyContactsKey)) { IsoSettings[MyContactsKey] = new List<ContactEDM>(); ;}
        }

        public static int GetMyId()
        {
            var myId = GetStoredObjectByKey(MyIdKey) as int?;
            return myId != null? myId.Value : 0;
        }

        public static void setMyId(int id)
        {
            SetStoredObjectByKey(MyIdKey, id);
            //IsoSettings[MyIdKey] = id;
        } 
       
        public static IList<TaskEDM> GetMyTasks()
        {
            var myTasks = GetStoredObjectByKey(MyTaskListKey) as IList<TaskEDM>;
            return myTasks;
        }

        public static bool AddMyTask(TaskEDM task)
        {
            if (task == null || task.TaskID <= 0) return false;

            var myTasks = GetStoredObjectByKey(MyTaskListKey) as IList<TaskEDM>;
            myTasks.Add(task);
            SetStoredObjectByKey(MyTaskListKey, myTasks);
            //IsoSettings[MyIdKey] = myTasks;
            return true;
        }
       
        public static IList<TaskEDM> GetOtherTasks()
        {
            var otherTasks = GetStoredObjectByKey(TaskListKey) as IList<TaskEDM>;
            return otherTasks;
        }

        public static bool AddOtherTask(TaskEDM task)
        {
            if (task == null || task.TaskID <= 0) return false;

            var otherTasks = GetStoredObjectByKey(TaskListKey) as IList<TaskEDM>;
            otherTasks.Add(task);
            SetStoredObjectByKey(TaskListKey, otherTasks);
            //IsoSettings[MyIdKey] = otherTasks;
            return true;
        }

        public static IList<ContactEDM> GetContacts()
        {
            var contacts = GetStoredObjectByKey(MyContactsKey) as IList<ContactEDM>;
            return contacts;
        }

        public static bool AddContact(ContactEDM contact)
        {
            if (contact == null || contact.ContactAccountId <= 0) return false;

            var contactList = GetStoredObjectByKey(MyContactsKey) as IList<ContactEDM>;
            contactList.Add(contact);
            SetStoredObjectByKey(MyContactsKey, contactList);
            //IsoSettings[MyContactsKey] = contactList;
            return true;
        }

        internal static void SaveOtherTasks(System.Collections.ObjectModel.ObservableCollection<ItemViewModel> OtherTasks)
        {
            if (OtherTasks != null)
            {
                var date = DateTime.Now;
                var listTasksEDM = (from item in OtherTasks
                                    select new TaskEDM
                                    {
                                        Description = item.LineOne,
                                        ObservationText = item.LineTwo,
                                        DueDate = DateTime.TryParse(item.LineThree, out date) ? date : DateTime.Now
                                    }).ToList();
            }
        }

        internal static void SaveMyTasks(System.Collections.ObjectModel.ObservableCollection<ItemViewModel> MyTasks)
        {
            if (MyTasks != null)
            {
                var date = DateTime.Now;              


                var listTasksEDM = (from item in MyTasks
                                    select new TaskEDM
                                    {
                                        Description = item.LineOne,
                                        ObservationText = item.LineTwo,
                                        DueDate = DateTime.TryParse(item.LineThree, out date) ? date : DateTime.Now
                                    }).ToList();

                SetStoredObjectByKey(MyTaskListKey, listTasksEDM);
            }
        }

        internal static void SaveContacs(System.Collections.ObjectModel.ObservableCollection<ItemViewModel> Contacts)
        {
            if (Contacts != null)
            {
                var listContatcts = (from item in Contacts
                                    select new ContactEDM
                                    {
                                        ContactAccountId = 0,
                                        Name = ""
                                    }).ToList();
            }
        }

        private static Object GetStoredObjectByKey(string key)
        {
            try
            {
                if (string.IsNullOrEmpty(key)) throw new Exception("Invalid object key");

                if (IsoSettings.Contains(key)) return IsoSettings[key];
                return null;
            }
            catch (Exception)
            {
                throw;
            }
        }

        private static void SetStoredObjectByKey(string key, object value)
        {
            try
            {
                if (string.IsNullOrEmpty(key)) throw new Exception("Invalid object key");
                IsoSettings[key] = value;

            }
            catch (Exception)
            {
                throw;
            }
        }

    }
}
